#include <gwbase/base/util.h>
#include <gwbase/base/test_harness.h>
#include <gwbase/base/test_util.h>
#include <dicom/upload/resume_info.h>

using namespace dicom;

class ResumeInfoTest {};

ResumeInfo GenFakeResumeInfo(int i) {
  ResumeInfo info;
  info.fid = StringPrintf("fid_%d", i);
  info.src_md5 = StringPrintf("src_md5_%d", i);
  info.location = StringPrintf("location_%d", i);
  info.offset = i;
  info.failed_count = i;
  info.insert_at = Timestamp::Now();
  return info;
}

bool SameResumeInfo(const ResumeInfo& f1, const ResumeInfo& f2) {
  return f1.fid == f2.fid && f1.src_md5 == f2.src_md5 &&
         f1.location == f2.location && f1.offset == f2.offset &&
         f1.insert_at == f2.insert_at;
}

TEST(ResumeInfoTest, SimpleTest) {
  Status s;
  ResumeInfoDb db;
  const char* dbpath = "_resume_info_test.db";
  dicom::DeleteFile(dbpath);
  s = db.OpenDb(dbpath);
  EXPECT_TRUE(s.ok());

  ResumeInfo f1 = GenFakeResumeInfo(1);
  db.UpdateResumeInfo(&f1);

  ResumeInfo f2;
  EXPECT_TRUE(db.GetResumeInfo(f1.fid, f1.src_md5, &f2));
  EXPECT_TRUE(SameResumeInfo(f1, f2));

  EXPECT_TRUE(db.GetResumeInfo("x", f1.src_md5, &f2) == false);
  EXPECT_TRUE(db.GetResumeInfo(f1.fid, "x", &f2) == false);

  f1.offset++;
  f1.failed_count++;
  f1.insert_at = Timestamp::Now();
  db.UpdateResumeInfo(&f1);
  ResumeInfo f3;
  EXPECT_TRUE(db.GetResumeInfo(f1.fid, f1.src_md5, &f3));
  EXPECT_TRUE(SameResumeInfo(f1, f3));

  db.DeleteResumeInfo(f1.fid, f1.src_md5);
  EXPECT_TRUE(db.GetResumeInfo(f1.fid, f1.src_md5, &f3) == false);
}

int main() {
  dicom::RunAllTests();
  return 0;
}
